package cn.xxstudy.visitshop.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import cn.xxstudy.visitshop.R
import cn.xxstudy.visitshop.databinding.ItemFooterBinding
import cn.xxstudy.visitshop.util.FooterState
import cn.xxstudy.visitshop.util.FooterStateData

/**
 * @author ：DELL on 2021/3/26 .
 * @packages ：cn.xxstudy.visitshop.adapter .
 * TODO:配合Paging的底部footerView
 */
class FooterAdapter(val retry: () -> Unit) : LoadStateAdapter<FooterAdapter.FooterViewHolder>() {

    override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {
        val binding = DataBindingUtil.getBinding<ItemFooterBinding>(holder.itemView)
        val state = when (loadState) {
            is LoadState.Loading -> FooterState.LOADING
            is LoadState.Error -> FooterState.ERROR
            else -> FooterState.NOT
        }
        binding?.retryBtn?.setOnClickListener { retry() }
        binding?.footerState = FooterStateData(state)
        binding?.executePendingBindings()
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): FooterViewHolder {
        val binding = DataBindingUtil.inflate<ItemFooterBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_footer,
            parent,
            false
        )
        return FooterViewHolder(binding.root)
    }

    inner class FooterViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView)
}